package com.itheima.controller;

import com.itheima.entity.User;
import com.itheima.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 获取所有用户
    @GetMapping("/users")
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(adminService.getAllUsers());
    }
    @GetMapping("/users/{username}")
    public ResponseEntity<User> getUsersByUsername(@PathVariable String username) {
        return ResponseEntity.ok(adminService.getUserByName(username));
    }
    @PutMapping("/users/{userName}")
    public ResponseEntity<String> updateUserRolePermissions(@PathVariable String userName) {
        return ResponseEntity.ok(adminService.updateUserIsSuper(userName));
    }
    // 修改用户角色权限
    @PutMapping("/users/{userName}/permissions")
    public ResponseEntity<String> updateUserRolePermissions(@PathVariable String userName,
                                                            @RequestBody Map<String, Boolean> permissions) {
        return ResponseEntity.ok(adminService.updateUserRolePermissions(userName, permissions));
    }

    // 重置用户密码
    @PutMapping("/users/{userName}/reset")
    public ResponseEntity<String> updateUserPassword(@PathVariable String userName
            , @RequestBody Map<String, String> requestBody) {
        String newPassword = requestBody.get("password");
        if (newPassword == null || newPassword.isEmpty()) {
            return ResponseEntity.badRequest().body("密码不能为空");
        }
        adminService.resetUserPassword(userName, newPassword);
        return ResponseEntity.ok("密码修改成功");
    }
}
